<?php
apf_require_controller("QaAbstract");
apf_require_class('Tools_Uploader');
apf_require_class('Const_QaDoc');
class Qa_Finance_QaNewController extends QaAbstractController{
    /**
     * 一级分类
     * @var string
     */
    private $tag;
    /**
     * 参数
     * @var unknown_type
     */
    private $params;
    public function handle_request(){
        $this->tag = Const_Qa::get_menu_list_en(Const_Qa::TAG_FINANCE);
        $this->params = $this->request->get_parameters();
        //权限判断
        if(!$_SESSION[$this->tag]){
            $this->show_tips("ERROR_PERMISSION", $_SERVER['HTTP_REFERER']);
        }
        if($_SERVER['REQUEST_METHOD'] == "GET"){ //显示新增或编辑页面
            $qa_id = intval($this->params['qid']);
            //跳转到修改页面
            if($qa_id){
                $data = $this->get_question_bll()->get_question_one($qa_id);
                $this->request->set_attribute('data', $data);
                $this->request->set_attribute('cate', Const_Qa::TAG_FINANCE);
            }
            //跳转到添加页面
            return "Qa_Finance_QaNew";
        }else{ //执行更新或插入
            if($this->params['formTitle']){ //ajax上传表单
                $this->params['formTitle'] = htmlspecialchars($this->params['formTitle']);
                echo $this->_upload_form($this->params['id']);
                exit(0);
            }else{//文件过大,无法post
                $result['msg'] = "上传失败,请上传小于20m的文件";
                $result['code'] = "0";
                echo json_encode($result);
                exit(0);
            }
        }
    }

    /**
     * replace财务表单
     * Enter description here ...
     */
    private function _upload_form($id = 0){
        $result['code'] = "0";
        //update数据
        if($id){
            //上传新文件
            if($_FILES[Const_QaDoc::INPUT_FILE_NAME]['name']){
                $up_result = $this->_upload_file();
                if($up_result['code']){ //上传成功
                    $doc_name = $up_result['doc_name'];
                }else{ //上传失败
                    return json_encode($up_result);
                }
            }
            //删除原表单文件
            $question = $this->get_question_bll()->get_question_one($id);
            if(!$question){
                $result['msg'] = "该表单不存在";
                return json_encode($result);
            }
            $old_file = Const_QaDoc::PATH_BASE . Const_QaDoc::PATH_UPLOAD . $question["file"];
            unlink($old_file);

            $addRes = $this->get_question_bll()->update_question($id, $this->params['formTitle'], $doc_name, null, $this->get_user_id());
            if($addRes){
                $result['code'] = "1";
                $result['msg'] = "修改成功";
            }
            return json_encode($result);
        }
        //插入新数据
        if($_FILES[Const_QaDoc::INPUT_FILE_NAME]['name']){
            $up_result = $this->_upload_file();
            if($up_result['code']){ //上传成功
                $doc_name = $up_result['doc_name'];
            }else{ //上传失败
                return json_encode($up_result);
            }
            //数据录入数据库
            $addRes = $this->get_question_bll()->add_question(Const_Qa::TAG_FINANCE, Const_Qa::TAG_FINANCE_BIAODAN, $this->params['formTitle'], $doc_name, $this->get_user_id());
            if($addRes){
                $result['code'] = "1";
                $result['msg'] = "上传成功";
            }
        }else{
            $result['msg'] = '请上传文件';
        }
        return json_encode($result);
    }

    /**
     * 上传文件
     * Enter description here ...
     */
    private function _upload_file(){
        $result['code'] = 0;
        //上传新文件
        $upDir = Const_QaDoc::PATH_BASE . Const_QaDoc::PATH_UPLOAD;
        $uploader = new Tools_Uploader(Const_QaDoc::INPUT_FILE_NAME);

        $uploader->setUpDir($upDir);
        $uploader->setValidExt(Const_QaDoc::get_valid_ext());
        //设置上传限制
        $uploader->setMaxSize(Const_QaDoc::MAX_SIZE * 1024 * 1024);

        //正常上传
        if($uploader->upload()){
            $doc_name = $uploader->getFileName();
            $result['code'] = 1;
            $result['doc_name'] = $doc_name;
        }else{
            $result['msg'] = $uploader->getErrorMsg();
        }
        return $result;
    }
}
